package com.ryh.shiro.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @ClassName RoleOrFilter
 * @Description 当配置的角色是多个时，实现只要当前用户满足任何一个角色即可。
 * @Author RaoYH
 */
public class RoleOrFilter extends AuthorizationFilter{
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
            throws Exception {
        //获取主体
        Subject subject = getSubject(request, response);

        String[] roles = (String[]) mappedValue;

        if (roles == null || roles.length == 0){
            return true;
        }

        //只需满足一个角色就返回true
        for (String role : roles) {
            if (subject.hasRole(role)){
                return true;
            }
        }
        return false;
    }
}
